<?php
/**
 * Clase Base FN para BaseMrpRecetaDetalleFN
 *
 * @package    
 * @subpackage BaseMrpRecetaDetalleFN
 * @author     Hans
 * @version    SVN:: BaseMrpRecetaDetalleFN.class.php,v 1.1 2012-03-07 02:02:08 Hans.Parra Exp $
 */
require($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/mrp/recetas/BN/MrpRecetaDetalleBN.class.php');
class BaseMrpRecetaDetalleFN extends MrpRecetaDetalleBN
{
  private static $fieldNames = array (
    'RcdInsumoId'=>'', 'RctRecetaId'=>'RCT_RECETAID', 'RcdInsumoCant'=>'', 'RcdUoM'=>'', 'RcdPorcMerma'=>'', 'RcdAlmacenId'=>'', 'SysInsUsr'=>'', 'SysInsAt'=>'', 'SysLstUpdUsr'=>'', 'SysLstUpdAt'=>''  
  );

  public function select()
  {
    try
    {

      $where = $this->buildConsult();
      $orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT rcd_insumoId, rct_recetaId, rcd_insumoCant, rcd_UoM, rcd_porcMerma, rcd_almacenId, SysInsUsr, SysInsAt, SysLstUpdUsr, SysLstUpdAt  FROM MRP_RECETADETALLE WHERE 1 ".$where." ".$orden;
      $result = mysql_db_query('main_db', $sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT rcd_insumoId, rct_recetaId, rcd_insumoCant, rcd_UoM, rcd_porcMerma, rcd_almacenId, SysInsUsr, SysInsAt, SysLstUpdUsr, SysLstUpdAt  FROM MRP_RECETADETALLE WHERE 1 ".$where;
      $result = mysql_db_query('main_db', $sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  private function buildConsult()
  {
    try
    {
      $where = "";
      
      if($this->getRcdInsumoId()){ $where .= " AND rcd_insumoId = '".$this->getRcdInsumoId()."'"; }
      if($this->getRctRecetaId()){ $where .= " AND rct_recetaId = '".$this->getRctRecetaId()."'"; }
      if($this->getRcdInsumoCant()){ $where .= " AND rcd_insumoCant = '".$this->getRcdInsumoCant()."'"; }
      if($this->getRcdUoM()){ $where .= " AND rcd_UoM = '".$this->getRcdUoM()."'"; }
      if($this->getRcdPorcMerma()){ $where .= " AND rcd_porcMerma = '".$this->getRcdPorcMerma()."'"; }
      if($this->getRcdAlmacenId()){ $where .= " AND rcd_almacenId = '".$this->getRcdAlmacenId()."'"; }
      if($this->getSysInsUsr()){ $where .= " AND SysInsUsr = '".$this->getSysInsUsr()."'"; }
      if($this->getSysInsAt()){ $where .= " AND SysInsAt = '".$this->getSysInsAt()."'"; }
      if($this->getSysLstUpdUsr()){ $where .= " AND SysLstUpdUsr = '".$this->getSysLstUpdUsr()."'"; }
      if($this->getSysLstUpdAt()){ $where .= " AND SysLstUpdAt = '".$this->getSysLstUpdAt()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";
      if(isset($this->sortCampos))
      {
          if($this->sortCampos || $this->sortCampos != "")
          {
            $a_valCampos = explode(";", $this->sortCampos);
            $a_valtype   = explode(";", $this->sortType);
            $order_by = " ORDER BY ";
            $a_order = array();
            foreach ($a_valCampos as $key => $val){
              $sTmpOrder = "";
              if(array_key_exists($val, self::$fieldNames)){
                $sTmpOrder .= self::$fieldNames[$val];
                switch ($a_valtype[$key]){
                  case 1: $sTmpOrder .= " ASC"; break;
                  case 2: $sTmpOrder .= " DESC"; break;
                }
                $a_order[] = $sTmpOrder;
              }
            }
            $order_by .= implode(", ", $a_order);
          }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT rcd_insumoId, rct_recetaId, rcd_insumoCant, rcd_UoM, rcd_porcMerma, rcd_almacenId, SysInsUsr, SysInsAt, SysLstUpdUsr, SysLstUpdAt  FROM MRP_RECETADETALLE WHERE 1 AND rcd_insumoId='".$this->getRcdInsumoId()."' AND rct_recetaId='".$this->getRctRecetaId()."'; ";
      $result = mysql_db_query('main_db', $sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
	$o_MrpRecetaDetalleFN = new MrpRecetaDetalleFN();
	$o_MrpRecetaDetalleFN->setRcdInsumoId($o_propel->rcd_insumoId);
	$o_MrpRecetaDetalleFN->setRctRecetaId($o_propel->rct_recetaId);
	$o_MrpRecetaDetalleFN->setRcdInsumoCant($o_propel->rcd_insumoCant);
	$o_MrpRecetaDetalleFN->setRcdUoM($o_propel->rcd_UoM);
	$o_MrpRecetaDetalleFN->setRcdPorcMerma($o_propel->rcd_porcMerma);
	$o_MrpRecetaDetalleFN->setRcdAlmacenId($o_propel->rcd_almacenId);
	$o_MrpRecetaDetalleFN->setSysInsUsr($o_propel->SysInsUsr);
	$o_MrpRecetaDetalleFN->setSysInsAt($o_propel->SysInsAt);
	$o_MrpRecetaDetalleFN->setSysLstUpdUsr($o_propel->SysLstUpdUsr);
	$o_MrpRecetaDetalleFN->setSysLstUpdAt($o_propel->SysLstUpdAt);
	return $o_MrpRecetaDetalleFN;
  }
}